<?php
function echoLeagueRow($team, $row) {
	echo getEchoTR($row, "center");
	echo "  <td class=\"strong\"><a name=\"$team->id\" />" . $row . "</td>\n";
	echo "  <td>" . getEchoValue($team->coach->getName()) . "</td>\n";
	echo "  <td>" . $team->getRosterLink() . "</td>\n";
	echo "  <td><a href=\"race_viewer.php?race_id=" . $team->race->id . "\">" . getEchoValue($team->race->name) . "</a></td>\n";
	echo "  <td>" . getEchoValue($team->getTR()) . "</td>\n";
	echo "  <td>" . getEchoValue($team->currentSeasonStats->played) . "</td>\n";
	echo "  <td>" . getEchoValue($team->currentSeasonStats->won) . "</td>\n";
	echo "  <td>" . getEchoValue($team->currentSeasonStats->drawn) . "</td>\n";
	echo "  <td>" . getEchoValue($team->currentSeasonStats->lost) . "</td>\n";
	echo "  <td>" . getEchoValue($team->currentSeasonStats->getNetTDs()) . "</td>\n";
	echo "  <td>" . getEchoValue($team->currentSeasonStats->getNetCas()) . "</td>\n";
	echo "  <td>" . getEchoValue($team->getPoints()) . "</td>\n";
	echo "  <td><font size=\"-2\">" . getEchoValue($team->lastUpdate) . "</font></td>\n";
}

function echoFixtureRow($fixture) {
	echo getEchoTR($fixture->round, "center");
	echo "  <td>" . $fixture->round . "</td>\n";
	echo "  <td>" . getEchoValue($fixture->homeTeam->getRosterLink()) . "<br />(" . getEchoValue($fixture->homeTeam->coach->getName()) . ")</td>\n";
	echo "  <td>vs</td>\n";
	echo "  <td>" . getEchoValue($fixture->awayTeam->getRosterLink()) . "<br />(" . getEchoValue($fixture->awayTeam->coach->getName()) . ")</td>\n";
	echo "</tr>\n";
}

include_once ("includes/init.php");
$title =$season->season . " standings";
include_once ("includes/header.php");

$sort= getParam("sort");
uasort($teams->teams, "conferenceSort");
switch ($sort) {
	case 1 :
		uasort  ($teams->teams, "coachSort");
		break;
	case 2 :
		uasort  ($teams->teams, "tvSort");
		break;
	case 3 :
		uasort  ($teams->teams, "playedSort");
		break;
	case 4 :
		uasort  ($teams->teams, "wonSort");
		break;
	case 5 :
		uasort  ($teams->teams, "drawnSort");
		break;
	case 6 :
		uasort ($teams->teams, "lostSort");
		break;
	case 7 :
		uasort ($teams->teams, "conferenceSort");
		break;
	case 8 :
		uasort ($teams->teams, "teamNameSort");
		break;
	case 9 :
		uasort ($teams->teams, "raceNameSort");
		break;
	case 10 :
		uasort ($teams->teams, "leagueNetTdsSort");
		break;
	case 11 :
		uasort ($teams->teams, "leagueNetCasSort");
		break;
	default :
		uasort ($teams->teams, "conferenceSort");
		break;

}     

$image = $season->getImage();
echo<<< EOQ
	<caption><h1>League Tables $season->season $image</h1></caption>
	<tr><td vAlign="top"><table border="0" cellPadding="5" cellSpacing="0" width="100%">
EOQ;

$lastConference = -1;
foreach ($teams->teams as $team) {
	if ($lastConference != $team->conference) {
		$i = 0;
		$lastConference = $team->conference;
		if (isset ($team->conference)) {
			$anchor = urlencode($team->conference);
			echo "<tr align=\"center\"><td colspan=\"13\"><br /><a name=\"$anchor\" /><h3>Conference: $team->conference</h3></td></tr>";
		}
		echo<<< EOQ
            <tr align="center">
              <th><a href="league.php?sort=7">#</a></th>
              <th><a href="league.php?sort=1">Coach</a></th>
              <th><a href="league.php?sort=8">Team</a></th>
              <th><a href="league.php?sort=9">Race</a></th>
              <th><a href="league.php?sort=2">Team Value</a></th>
              <th><a href="league.php?sort=3">Played</a></th>
              <th><a href="league.php?sort=4">Won</a></th>
              <th><a href="league.php?sort=5">Drawn</a></th>
              <th><a href="league.php?sort=6">Lost</a></th>
              <th><a href="league.php?sort=10">Net TD</a></th>
              <th><a href="league.php?sort=11">Net Cas</a></th>
              <th><a href="league.php?sort=7">Points</a></th>
              <th><font size="-2">Updated</font></th>
              <th>&nbsp;</th>
            </tr>

EOQ;
	}
	echoLeagueRow($team, ++ $i);
	if (isset ($loggedin) and $loggedin->canUpdateTeam($team)) {
		if ($team->usesTeamEditor()) {
			echo "<td><a href=\"team_editor.php?id=$team->id\"><img src=\"$rootDir/images/edit.jpg\" title=\"Edit Team\"/></a></td>";
		} else {
			echo "<td><a href=\"team_creator.php?team_id=" . $team->id . "\"><img src=\"$rootDir/images/edit.jpg\" title=\"Create Team\"/></a></td>";
		}
	}
	echo "</tr>";
}

echo<<< EOQ
  </table></td></tr>
          <tr>
          	<td colspan="2">
                <h5>$season->season</h5>
                <p>$season->comments</p>
          </td>
        </tr>

EOQ;
if ($season->winner) {
		$winnerLink = $season->getWinnerLink($teams);
		$runnerLink = $season->getRunnerUpLink($teams);
echo<<< EOQ
	<tr><th align="left">Winner: $winnerLink</th></tr>
	<tr><th align="left">Runner Up: $runnerLink</th></tr>
EOQ;
}
include ("includes/footer.php");
?>
